
local CellHelper = {}

---创建一个cell
function CellHelper.createOneCell()
	local cell=FZutil.createCellNode("TradeUIall/TradeNodeSmall")
    CellHelper.innerInit(cell)
	return cell
end

---用一个已经存在的ItemIcon的node初始化一个cell出来
function CellHelper.initWithNode(node)
	local cell=FZutil.createMap(node)
    CellHelper.innerInit(cell)
	return cell
end

---初始化内部
function CellHelper.innerInit(cellmap)
    cellmap.Image_icon:setTouchEnabled(true)
    cellmap.Image_icon:setSwallowTouches(true)
    cellmap.Image_icon:ignoreContentAdaptWithSize(true)
    cellmap.Text_num_N:setString("")
    cellmap.Node_amount:hide()
    cellmap.Image_popular:hide()
    cellmap.Image_select:hide()
    cellmap.Image_add:hide()
end

---
--设置格子
--@param cellmap cell 格子的map
--@param data item_data 背包里的数据
function CellHelper.setdata(cellmap,data)
	if data==nil then
        cellmap.Image_icon:hide()
        cellmap.Text_name:setString("")
        cellmap.Text_num_N:setString("")
        --cellmap.Image_frame:loadTexture(IconPath.cellQualityIcon(0))
        cellmap.Image_popular:hide()
        cellmap.Image_select:hide()
        cellmap.Image_add:hide()
    else
        CellHelper.setCellData(cellmap,data.item_id,data.item_type,data.number)
	end
end

---设置格子，number为空字符串时不显示数量
--@param cellmap cell 格子控件
--@param item_id int 物品ID
--@param cell_type Cell_Type 为constant.Cell_Type定义的枚举
--@param number number 数量
function CellHelper.setCellData(cellmap,item_id,cell_type,number)
    if cell_type == Cell_Type.STORE or cell_type == Cell_Type.TPACKS then
        cellmap.Text_name:hide()
        cellmap.Image_frame:hide()
    elseif cell_type == Cell_Type.TPACK then
        cellmap.Image_select:hide()
    end
    local item_type=constant.get_Item_Type_by_celltype(cell_type)
    CellHelper.setCell(cellmap,item_type,item_id,number)
end

---设置格子
--@param cellmap cell 格子控件
--@param item_type item_type 为constant.item_type定义的枚举
--@param item_id int 物品ID
--@param number number 数量
function CellHelper.setCell(cellmap,item_type,item_id,number)
    cellmap.Image_icon:show()
    local ret=CellHelper.frameReward(item_type,item_id)
    cellmap.Image_icon:loadTexture(ret.type_icon)
    cellmap.Image_frame:loadTexture(ret.quality_icon)
    cellmap.Text_name:setString(ret.cfg.name)
    if item_type==Item_Type.IT_EQUIP then
        cellmap.Text_num_N:setString("") 
    else
        if number == -1 then
            cellmap.Text_num_N:setString(Language.infinite)
        else
            cellmap.Text_num_N:setString(number)
            CellHelper.setGray(cellmap,false)
            CellHelper.setGray(cellmap,number==0)
        end
    end   
    return ret
end

--设置格子的品质框
function CellHelper.frameReward(item_type,item_id)
    local table_data = {cfg = {},type_icon="",quality_icon="",quality_bg=""}
    if item_type == Item_Type.IT_PROP then
        table_data.cfg = configData.cfg_items[item_id]
        table_data.type_icon = IconPath.itemIcon(table_data.cfg.icon)  
    end
    if table_data.cfg.quality then
        table_data.quality_icon = IconPath.cellQualityIcon(table_data.cfg.quality)
        --table_data.quality_bg   = IconPath.cellQualityBg(table_data.cfg.quality)
    else 
        table_data.quality_icon = IconPath.cellQualityIcon(5)   --橙色
        --table_data.quality_bg   = IconPath.cellQualityBg(5)
    end

    return table_data
end

---设置装备图标，不显示数量
function CellHelper.setEquip(cellmap,equip_id)
    CellHelper.setCellData(cellmap,equip_id,Item_Type.IT_EQUIP,"")
end

function CellHelper.setMoneyItem( cellmap , item_id , need_num , own_num )
    CellHelper.setCell(cellmap,Item_Type.IT_COINS,item_id,need_num)
    if own_num>=need_num then --绿色
        cellmap.AtlasLabel_amount:setColor(cc.c3b(0,255,0))
    else --红色
        cellmap.AtlasLabel_amount:setColor(cc.c3b(255,0,0))
    end
end

function CellHelper.setGray(cellmap,gray)
    if gray then
        cellmap.Image_icon:getVirtualRenderer():setState(1)   
    else
        cellmap.Image_icon:getVirtualRenderer():setState(0)
    end
end

return CellHelper